﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using ServisniList.Core;

namespace ServisniList.Rukovatelji
{
    public class ExportServisnogListaUPdfAdapter
    {
        public static void Export(Core.List trenutniList, string path)
        {
            Document doc = new Document(PageSize.A4);
            var output = new FileStream(path, FileMode.Create);
            var writer = PdfWriter.GetInstance(doc, output);
            doc.Open();

            #region Pozadina

            var pozadina = iTextSharp.text.Image.GetInstance("ServisniListUnos.png");
            pozadina.SetAbsolutePosition(0, 0);
            pozadina.ScaleAbsoluteHeight(PageSize.A4.Height);
            pozadina.ScaleAbsoluteWidth(PageSize.A4.Width);
            doc.Add(pozadina);

            #endregion

            #region Document data generation

            PdfContentByte cb = writer.DirectContent;
            BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
            cb.SaveState();

            dodajText(trenutniList.DatumPrimitka.ToShortDateString(), cb, bf, new Point(25, 760));
            dodajText(trenutniList.ZaprimljenoUPnp ? "U PNP" : "Na adresi korisnika", cb, bf, new Point(230, 760));
            dodajText(Rukovatelji.Korisnici.GeneriajPrikazKorisnika(trenutniList.Zaprimatelj), cb, bf, new Point(400, 760));
            dodajText(trenutniList.BrojLista, cb, bf, new Point(400, 800));
            dodajText(trenutniList.Stranka.Adresa, cb, bf, new Point(400, 725));
            dodajText(trenutniList.Stranka.BrojTelefona, cb, bf, new Point(400, 690));
            dodajText(trenutniList.Stranka.BrojMobitela, cb, bf, new Point(400, 660));

            dodajText(trenutniList.Stranka.Naziv, cb, bf, new Point(25, 690));
            dodajText(trenutniList.Stranka.OIB, cb, bf, new Point(150, 690));

            var opisiProblema = trenutniList.ListOpisProblemaStavkas.ToList();

            int height = 610;
            int width = 60;

            foreach (var item in opisiProblema)
            {
                dodajText(item.OpisProblema.Naziv, cb, bf, new Point(width, height));
                width += 60;
            }

            dodajText(trenutniList.OpisProblema, cb, bf, new Point(60, 590),10); 

            height = 540;

            formatirajOpisRada(trenutniList.OpisRada, 525, cb, bf);

            if (trenutniList.OpisRadaRadUServisu)
            {
                dodajText("Rad u servisu", cb, bf, new Point(430, height));
            }
            else
            {
                dodajText("Rad na terenu", cb, bf, new Point(430, height));
            }


            if (trenutniList.Serviser != null)
            {
                dodajText(Rukovatelji.Korisnici.GeneriajPrikazKorisnika(trenutniList.Serviser), cb, bf, new Point(120, 390));
            }

            dodajText(trenutniList.UtrosenoVrijemeRada.ToString(), cb, bf, new Point(500, 390));

            height = 350;

            foreach (ListMaterijalStavka stavka in trenutniList.ListMaterijalStavkas)
            {
                if (stavka.Komada > 0)
                {
                    height -= 20;
                    dodajText(stavka.Materijal.Naziv, cb, bf, new Point(60, height));
                    dodajText(stavka.Komada.ToString(), cb, bf, new Point(450, height));
                    dodajText("komad/a", cb, bf, new Point(500, height));
                }
            }


            if (trenutniList.DatumIsporuke != null)
            {
                dodajText(((DateTime)trenutniList.DatumIsporuke).ToShortDateString(), cb, bf, new Point(25, 50));
                dodajText(Rukovatelji.Korisnici.GeneriajPrikazKorisnika(trenutniList.Isporucitelj), cb, bf, new Point(150, 50));
                dodajText(trenutniList.IsporukaUPnp ? "U P.N.P." : "Na adresi korisnika", cb, bf, new Point(420, 40));
            }


            cb.RestoreState();

            #endregion

            doc.Close();

        }

        private static void formatirajOpisProblema(string opisProblema, PdfContentByte cb, BaseFont bf)
        {
            if (opisProblema == null)
            {
                return;
            }

            int initialWidth = 300;
            int width = initialWidth;
            int intialHeight = 600;
            int height = intialHeight;

            string[] redovi = opisProblema.Split(Environment.NewLine.ToCharArray());

            foreach (string trenutniString in redovi)
            {
                if (!string.IsNullOrEmpty(trenutniString))
                {
                    if (height < 520)
                    {
                        height = intialHeight;
                        width += 180;
                    }
                    height -= 15;
                    dodajText(trenutniString, cb, bf, new Point(width, height), 8);
                }
            }

        }

        private static void formatirajOpisRada(string inputOpisRada, int height, PdfContentByte cb, BaseFont bf)
        {
            if (inputOpisRada == null)
            {
                return;
            }

            int intitalHeight = height;
            int width = 30;
            string opisRada = inputOpisRada;
            string[] redovi = opisRada.Split(Environment.NewLine.ToCharArray());
            foreach (string trenutniString in redovi)
            {
                if (!string.IsNullOrEmpty(trenutniString))
                {
                    if (height < 440)
                    {
                        height = intitalHeight;
                        width += 180;
                    }
                    height -= 15;
                    dodajText(trenutniString, cb, bf, new Point(width, height), 8);
                }
            }
        }

        private static void dodajText(string sadrzaj, PdfContentByte cb, BaseFont bf, Point kordinate)
        {
            dodajText(sadrzaj, cb, bf, kordinate, 12);
        }

        private static void dodajText(string sadrzaj, PdfContentByte cb, BaseFont bf, Point kordinate, float fontSize)
        {
            cb.BeginText();
            cb.MoveText(kordinate.X, kordinate.Y);
            cb.SetFontAndSize(bf, fontSize);
            cb.ShowText(sadrzaj);
            cb.EndText();
        }
    }
}
